<template>
  <div class="s-canvas" @click="changeCode">
    <canvas ref="canvas" :width="contentWidth" :height="contentHeight"></canvas>
  </div>
</template>

<script>
export default {
  name: "SIdentify",
  props: {
    identifyCodes: {
      type: String,
      // default: "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ",
      default: "123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
    },
    fontSizeMin: {
      type: Number,
      default: 40
    },
    fontSizeMax: {
      type: Number,
      default: 45
    },
    backgroundColorMin: {
      type: Number,
      default: 200
    },
    backgroundColorMax: {
      type: Number,
      default: 220
    },
    dotColorMin: {
      type: Number,
      default: 60
    },
    dotColorMax: {
      type: Number,
      default: 120
    },
    contentWidth: {
      type: Number,
      default: 125
    },
    contentHeight: {
      type: Number,
      default: 50
    }
  },
  data() {
    return {
      identifyCode: ""
    };
  },
  methods: {
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    randomColor(min, max) {
      const r = this.randomNum(min, max);
      const g = this.randomNum(min, max);
      const b = this.randomNum(min, max);
      return `rgb(${r},${g},${b})`;
    },
    drawPic() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext("2d");
      ctx.textBaseline = "bottom";

      // 绘制背景
      ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax);
      ctx.fillRect(0, 0, this.contentWidth, this.contentHeight);

      // 绘制文字
      const charSpacing = this.contentWidth / (this.identifyCode.length + 1);
      const offset = charSpacing / 2; // 调整字符位置靠左
      for (let i = 0; i < this.identifyCode.length; i++) {
        this.drawText(ctx, this.identifyCode[i], charSpacing, offset, i);
      }
      // 绘制干扰线和干扰点
      // this.drawLine(ctx);
      // this.drawDot(ctx);
    },
    drawText(ctx, txt, charSpacing, offset, i) {
      ctx.fillStyle = this.randomColor(50, 160);
      ctx.font = `${this.randomNum(this.fontSizeMin, this.fontSizeMax)}px SimHei`;
      const x = i * charSpacing + offset; // 调整字符位置靠左
      const y = this.randomNum(this.fontSizeMax, this.contentHeight - 5);
      const deg = this.randomNum(-20, 20); // 限制旋转角度范围

      ctx.save();
      ctx.translate(x, y);
      ctx.rotate((deg * Math.PI) / 180);
      ctx.fillText(txt, 0, 0);
      ctx.restore();
    },
    drawLine(ctx) {
      for (let i = 0; i < 4; i++) {
        ctx.strokeStyle = this.randomColor(100, 200);
        ctx.beginPath();
        ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight));
        ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight));
        ctx.stroke();
      }
    },
    drawDot(ctx) {
      for (let i = 0; i < 30; i++) {
        ctx.fillStyle = this.randomColor(0, 255);
        ctx.beginPath();
        ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI);
        ctx.fill();
      }
    },
    changeCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(identifyCodes, length) {
      for (let i = 0; i < length; i++) {
        this.identifyCode += identifyCodes[this.randomNum(0, identifyCodes.length)];
      }
      this.$emit("update:changeCode", this.identifyCode);
    }
  },
  watch: {
    identifyCode() {
      this.drawPic();
    }
  },
  mounted() {
    this.changeCode();
  }
};
</script>

<style scoped>
.s-canvas {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  cursor: pointer;
}
</style>
